const TABLEName = "file_info";
const { querySql, queryOne } = require("../utils/sql");
const { setLog } = require("../utils/log.js");

const update = async (req, res) => {
  try {
    let { id, fileName } = req.body;
    const sqpStr = `UPDATE ${TABLEName} SET fileName = '${fileName}' WHERE id = '${id}'`;
    let result = await querySql(sqpStr);
    // console.log("result", result);
    if (result.affectedRows == 1) {
      setLog(
        req.getToken()?.id,
        `用户${
          req.getToken()?.username
        }将文件id（${id}）的文件名修改为了：${fileName}`
      );
      res.json({
        code: 0,
        data: null,
        msg: "修改成功。",
      });
    } else {
      res.json({
        code: -1,
        data: null,
        msg: "修改失败。",
      });
    }
  } catch (err) {
    res.status(500).json({
      code: -1,
      data: null,
      msg: "服务器错误。",
    });
  }
};
const del = async (req, res) => {
  try {
    let { id } = req.body;
    const sqpStr = `DELETE FROM ${TABLEName} WHERE id= '${id}'`;
    let result = await querySql(sqpStr);
    // console.log("result", result);
    if (result.affectedRows == 1) {
      setLog(
        req.getToken()?.id,
        `用户${
          req.getToken()?.username
        }将文件id（${id}）的文件删除了`
      );
      res.json({
        code: 0,
        data: null,
        msg: "删除成功。",
      });
    } else {
      res.json({
        code: -1,
        data: null,
        msg: "删除失败。",
      });
    }
  } catch (err) {
    res.status(500).json({
      code: -1,
      data: null,
      msg: "服务器错误。",
    });
  }
};

module.exports = {
  update,
  del,
};
